package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.ImageDao;
import com.fuli.app.dto.ImageDTO;
import com.fuli.app.entity.Image;
import com.fuli.app.service.ImageService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class ImageServiceImpl implements ImageService {

    @Autowired
    private ImageDao imageDao;
    @Value("${upload.image.path}")
    private String imagePath;
    @Value("${upload.image.dir}")
    private String imageDir;
    @Value("${upload.image.trash}")
    private String imageTrash;

    @Override
    public ActionResult<ImageDTO> upload(MultipartFile file, String description, String classId) {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf('.'));
        Image po = EntityUtils.init(Image.class);
        String imageName = po.getId() + suffix;
        po.setName(filename);
        po.setUrl(imagePath + imageName);
        po.setAbsolutePath(imageDir + imageName);
        po.setClassId(classId);
        po.setDescription(description);

        File dest = new File(po.getAbsolutePath());
        //判断目录是否存在
        if (!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            System.out.println("保存图片异常");
            e.printStackTrace();
        }
        imageDao.insert(po);
        return new ActionResult<>(EntityUtils.copyObject(po, ImageDTO.class));
    }

    @Override
    public ActionResult<String> delete(String id) {
        Image po = imageDao.get(id);
        if (po == null)
            return new ActionResult<>(-1, "删除失败，不存在id=" + id + "的数据");

        String destPath = imageTrash + po.getId() + po.getName().substring(po.getName().lastIndexOf('.'));
        File dest = new File(destPath);
        if (!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        File file = new File(po.getAbsolutePath());
        if (file.exists()) {
            if (file.equals(dest)) {
                file.delete();
            } else {
                file.renameTo(dest);
            }
        }
        imageDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<ImageDTO> get(String id) {
        Image image = imageDao.get(id);
        ImageDTO dtos = EntityUtils.copyObject(image, ImageDTO.class);
        return new ActionResult<>(dtos);
    }

    @Override
    public ActionResult<List<ImageDTO>> getByClassId(String id) {
        List<Image> images = imageDao.getByClassId(id);
        List<ImageDTO> dtoList = EntityUtils.copyList(images, ImageDTO.class);
        return new ActionResult<>(dtoList);
    }

    @Override
    public ActionResult<Pagination<ImageDTO>> pageQuery(String classId, Integer pageNo, Integer pageSize) {
        Pagination<ImageDTO> pagination = new Pagination<>(pageNo, pageSize);
        int count = imageDao.countByClassId(classId);
        pagination.setCount(count);
        if (count > 0) {
            List<Image> poList = imageDao.listByClassId(classId, pagination.getBeginIndex(), pagination.getPageSize());
            pagination.setRows(EntityUtils.copyList(poList, ImageDTO.class));
        }
        return new ActionResult<>(pagination);
    }

}
